package com.taoyibao.hongcanting.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.utils.AmountFilter;

/**
 * @author TangAnna
 * @description: 金额的输入框
 * @date :${DATA} 13:17
 */
public class AmountEditText extends AppCompatEditText {

    /**
     * 提示文字的大小  默认值都是28像素值
     */
    private float mHintTextSize;
    /**
     * 正常文字的大小
     */
    private float mTextSize;

    public AmountEditText(Context context) {
        super(context);
    }

    public AmountEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initArrt(context, attrs);
    }

    /**
     * 获取自定义属性
     *
     * @param context
     * @param attrs
     */
    private void initArrt(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AmountEditText );
        mHintTextSize = typedArray.getDimensionPixelSize(R.styleable.AmountEditText_amountHintTextSize, 14);
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.AmountEditText_amountTextSize, 14);
        this.setTextSize(mHintTextSize);
        this.addTextChangedListener(mTextWatcher);
        typedArray.recycle();
    }

    public AmountEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initArrt(context, attrs);

    }

    @Override
    public void setFilters(InputFilter[] filters) {
        filters = new InputFilter[]{new AmountFilter()};
        super.setFilters(filters);
    }


    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!TextUtils.isEmpty(AmountEditText.this.getText().toString().trim())) {
                AmountEditText.this.setTextSize(mTextSize);
            } else {
                AmountEditText.this.setTextSize(mHintTextSize);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}
